package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.common.base.ServerResponse;
import com.bifrost.entity.User;
import com.bifrost.model.ResultModel;
import com.bifrost.model.ro.CustomerRO;
import com.bifrost.model.vo.CustomerVO;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.CustomerManageService;
import com.bifrost.util.QueryUtils;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员管理相关接口
 */
@RestController
@RequestMapping(value = "/admin/customerManage")
@Api("后台会员管理相关接口")
public class CustomerManageController {

    @Autowired
    private CustomerManageService customerManageService;
    @Autowired
    private AdminUserService userService;

    @GetMapping("/list/customers")
    @ApiOperation("所有会员基本信息展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" , value = "查询条件(姓名)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "isActive" , value = "查询条件(是否激活：激活为true,未激活为false)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "customerTypeName" , value = "查询条件(会员类型名称)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "loyaltyCard" , value = "查询条件(会员卡号)",dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "pageCode" , value = "当前页(默认为第一页)" , dataType = "int" , paramType = "query"),
            @ApiImplicitParam(name = "pageSize" , value = "单页显示条数(默认为10个)" ,  dataType = "int" , paramType = "query")
    })
    public ResultModel showCustomersWithList(@CurrentAdmin Long currentUser, String name, String isActive, String customerTypeName, String loyaltyCard, Long[] merchantStoreIds, @RequestParam(defaultValue = "1") Integer pageCode, @RequestParam(defaultValue = "10") Integer pageSize){
        try{
            List<Long> merchantStoreIdList = Lists.newArrayList();
            merchantStoreIdList = QueryUtils.getMerchantIds(userService.findById(currentUser) ,merchantStoreIds);
            ServerResponse response = customerManageService.findCustomerModels(name,isActive,customerTypeName,loyaltyCard,merchantStoreIdList,pageCode,pageSize);
            if(response.isSuccess()){
                return ResultModel.ok().put("customerList",response.getData());
            }
            return ResultModel.error(response.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return ResultModel.error("出现一些未知错误");
        }
    }

    @PostMapping("/customer")
    @ApiOperation("根据会员准确属性查询一个会员信息")
    public ServerResponse customer(@RequestBody CustomerRO customerRO){
        try{
            CustomerVO customerVO = customerManageService.findOneCustomer(customerRO);
            if(customerVO == null){
                return ServerResponse.createByErrorMsg("输入信息有误");
            }
            return ServerResponse.createBySuccess(customerVO);
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("出现一些未知错误");
        }
    }

}
